﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using com.crawler.common;
using com.crawler.components;

namespace com.crawler.winfrom
{
    public partial class FrmTest : Form
    {
        /// <summary>
        /// 系统配置信息
        /// </summary>
        private SysConfigInfo m_sys_config = null;

        /// <summary>
        /// 站点列表
        /// </summary>
        List<BookSiteInfo> m_list = null;

        public FrmTest()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
            try
            {
                m_sys_config = SerializationHelper.Load(typeof(SysConfigInfo), Consts.SysConfigFileName) as SysConfigInfo;

                m_list = SerializationHelper.Load(typeof(List<BookSiteInfo>), Consts.SiteFileName) as List<BookSiteInfo>;

            }
            catch (Exception ex)
            {
                LogHelper.Error("加载系统配置失败" + ex.ToString());
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {

         

            //BaiDuManager t_seo_mgr = new BaiDuManager();

            //System.Threading.Thread t_thd = new System.Threading.Thread(t_seo_mgr.GetTOP123Book);
            //t_thd.Start();

            //string t_key = txt_key.Text.Trim();

            BaiDuManager t_baidu_mgr = new BaiDuManager();
            t_baidu_mgr.StartContent();

            /*
            BaiDuChapter t_baidu_chapter = new BaiDuChapter();

         

            t_baidu_chapter.m_list = m_list;
            t_baidu_chapter.m_sys_config = m_sys_config;

            //long t_book_id=0;
            //if(!long.TryParse(txt_key.Text.Trim(),out t_book_id))
            //    t_book_id=0;


            DBManager t_db_mgr = new DBManager();

            //BookInfo t_book=t_db_mgr.GetBookInfoById(55);

           // t_baidu_chapter.StartBook(t_book);

            t_baidu_chapter.StartSEOChatper(55,21);

            ChapterContentManager t_chapter_content_mgr = new ChapterContentManager();
           // t_chapter_content_mgr.GetContentBySign(11, 136, "http://www.123du.net/dudu-38/1031718/9471497.html", 9312, "<Div id=\"DivContent\" title=\"工号2783号编辑：欢迎您常来123读 www.123du.net |3.2181752", "</div>");


            MessageBox.Show("完成");


            //Queue<BookInfo> t_list = t_db_mgr.GetSEOBook();
            //int index = 0;

            //while (t_list != null && t_list.Count > 0)
            //{
            //    BookInfo t_book = t_list.Dequeue();
            //    index++;
            //    if (t_book.BookId == 554)
            //    {
            //        LogHelper.Info("dequeue index=" + index.ToString());
            //        t_baidu_chapter.StartBook(t_book);
            //    }
            //}

           
        

            //MessageBox.Show(t_book.BookId.ToString());
            //t_baidu_chapter.StartBook(t_book);*/
        }

        private void btn_icon_Click(object sender, EventArgs e)
        {
            //System.Threading.Thread t_thd = new System.Threading.Thread(CrawlerICON);
            //t_thd.Start();

            NetSiteCatchManager.DownBookICON("http://www2.photoshop520.com/picture/201082512139921.jpg",1,"F:\\pic");
            MessageBox.Show("download");
        }

        private void CrawlerICON()
        {
            DBManager t_db_mgr = new DBManager();
            List<ChapterInfo> t_chapter_list = null;
            BookInfo t_book=null;

            BaiDuChapter t_baidu_chapter = new BaiDuChapter();
            t_baidu_chapter.m_list = m_list;
            t_baidu_chapter.m_sys_config = m_sys_config;

            try
            {
                List<BookUrlInfo> t_list = t_db_mgr.GetICONBookList();

                if (t_list == null || t_list.Count <= 0)
                {
                    txt_result.AppendText("没有获取到小说信息");
                    return;
                }

                txt_result.AppendText(string.Format("需要收录{0}本小说", t_list.Count));

                string t_book_icon_url = string.Empty;

                int index = 0;

                foreach (BookUrlInfo t_url in t_list)
                {
                    if (index < 351)
                    {
                        index++;
                        continue;
                    }
                    if (t_url.SartIndex == -1)
                        continue;
                    string t_html = HttpHelper.GetHttpRequestStringByNUll_Post(t_url.ChapterListUrl);

                    if (!string.IsNullOrEmpty(t_html))
                    {
                        System.Text.RegularExpressions.Match t_mc = System.Text.RegularExpressions.Regex.Match(t_html, "<div[^<>]*?pic_box[^<>]*?>\\s*?<a[^<>]*?>\\s*?<img[^<>]*?src=s*[\'\"]*([^\"\']*)[\'\"]*[^<>]*?>\\s*?</a>\\s*?</div>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);

                        if (t_mc != null && t_mc.Groups.Count > 0)
                        {
                            t_book_icon_url = t_mc.Groups[1].Value.ToString();

                            if (!string.IsNullOrEmpty(t_book_icon_url))
                            {
                                t_db_mgr.UpdateBookUrl(t_url.BookId, t_book_icon_url);
                            }
                        }
                    }

                    t_chapter_list = t_db_mgr.GetChapterByBookId(t_url.BookId);

                    if (t_chapter_list == null || t_chapter_list.Count == 0)
                    {
                        t_book = t_db_mgr.GetBookInfoById(t_url.BookId);
                        if (t_book != null)
                        {
                            t_baidu_chapter.StartBook(t_book);

                            LogHelper.Info("start crawler chapter title for bookid=" + t_book.BookId.ToString());
                        }
                    }

                    index++;
                    LogHelper.Info("complated book index=" + index.ToString());
                }

                txt_result.AppendText("全部更新完成");
            }
            catch (Exception ex)
            {
                txt_result.Text = ex.ToString();
            }
        }


    }
}
